package com.zhq.widget.popupwindow;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhq.R;
import com.zhq.widget.SpinView;


/**
 * Created by Administrator on 2017/6/5.
 */

public class LoadingView extends Dialog {
    private static final float ANIMATE_SPEED = 1;
    private RelativeLayout loadLayout;
    private TextView tvContent;
    private String content;
    private int alpha, visible;
    private static float scale;

    public LoadingView(Context context) {
        super(context, R.style.loading_dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popupwindow_pregress);
        loadLayout = findViewById(R.id.relative_progress);
        tvContent =  findViewById(R.id.tv_alert);
        loadLayout.getBackground().setAlpha((int) (250 * 0.8));
        SpinView mIndeterminateView = new SpinView(getContext());
        mIndeterminateView.setAnimationSpeed(ANIMATE_SPEED);
        int widthMeasureSpec = dpToPixel(40, getContext());
        int heightMeasureSpec = dpToPixel(40, getContext());
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(widthMeasureSpec, heightMeasureSpec);
        rlp.addRule(RelativeLayout.CENTER_IN_PARENT);//addRule参数对应RelativeLayout XML布局的属性
        loadLayout.addView(mIndeterminateView, rlp);
        setCanceledOnTouchOutside(false);
    }

    public int dpToPixel(float dp, Context context) {
        if (scale == 0) {
            scale = context.getResources().getDisplayMetrics().density;
        }
        return (int) (dp * scale);
    }

    /**
     * 显示进度条
     * 计时器500毫秒，防止activity未初始化，加载popupWindow,导致popupWindow不显示
     **/
    public void showProgress() {
        if (!isShowing()) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    show();
                }
            }, 500);
        }
    }


    public void showPopupWindow() {
        if (!isShowing()) {
            show();
        }
    }


    public interface LoadingOnListener {
        void showLoading();
    }

    /**
     * 显示进度条
     * 计时器500毫秒，防止activity未初始化，加载popupWindow,导致popupWindow不显示
     * onListener：进度条加载监听
     **/
    public void showLoadingOnListener(final LoadingOnListener onListener) {
        if (!isShowing()) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    show();
                    if (onListener != null) {
                        onListener.showLoading();
                    }
                }
            }, 500);
        }
    }

    /**
     * 0到255
     *
     * @param value
     */
    public void setLoadAlpha(int value) {
        this.alpha = value;

    }

    @Override
    public void show() {
        super.show();
        if (!TextUtils.equals(content, "")) {
            tvContent.setVisibility(View.VISIBLE);
            tvContent.setText(content);
        }
        if (visible == View.GONE) {
            tvContent.setVisibility(visible);
        }
        if (alpha != 0) {
            loadLayout.getBackground().setAlpha(alpha);
        }
    }

    /**
     * @param content
     */
    public void setLoadContent(String content) {
        this.content = content;

    }

    public void showOrHideLoadContent(int visible) {
        this.visible = visible;
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}
